﻿using Microsoft.VisualBasic; 
using System;
using System.Windows.Forms;
using IESN.BusinessCore;
using Microsoft.VisualBasic.CompilerServices;
using System.Data;
using IESN.DAL;
using System.Collections.Generic;
using IESN.Enum;
using IESN.DM;

namespace IESN.BusinessCore
{
    public class Initialization
    {
        #region 属性

        /// <summary>
        /// 控制器
        /// </summary>
        private static Controller mController;
        public static Controller MController
        {
            get
            {
                return mController;
            }
        } 
         //<summary>
         //?????????????
         //</summary>
        public static short NumBoard = 12; 
        /// <summary>
        /// 有可能是批次 数
        /// </summary>
        public static short NumLot = 12;
        /// <summary>
        /// 天车程序数量
        /// </summary>
        public static short NumXPPrg = 5;
        /// <summary>
        /// 天车步骤数量
        /// </summary>
        public static short NumXPStep = 50; 
        /// <summary>
        /// 运行周期 内存存放地址
        /// </summary>
        public static short DMRunCycle = 8;
        public static short IsNewPrg = 0;
        /// <summary>
        /// 程序开始地址？？？
        /// </summary>
        public static int UsrStartAdd = 3000;//0xbb8;
        /// <summary>
        /// 程序开始地址　？？
        /// </summary>
        public static int ProgStart = 3001; 
        /// <summary>
        /// 程序周期时间 存放地址
        /// </summary>
        public static int ProgramCycleTimeAddr = 2390;
        /// <summary>
        /// 由DMS产生的Pcode
        /// </summary>
        public static short PcodeGenByDMS;
        /// <summary>
        /// 初始化 地址
        /// </summary>
        public static short initAddr;
        /// <summary>
        /// 天车显示
        /// </summary>
        public static short intXP_Display;

        public static short intPcodeArea;
        
        /// <summary>
        /// 锡
        /// </summary>
        public static short TIN_1000 = 0;
        public static short NI_1000 = 0;
        public static short NI_2000 = 0;
        public static short NI_4000 = 0;

        public static short CU_1000 = 0;
        public static short CU_2000 = 0;
        public static short CU_3000 = 0;
        public static short CU_4000 = 0;

        public static short timerPeriod;

        public static bool isServer = true;
        /// <summary>
        /// 天车上升时间
        /// </summary>
        public static short XP_UP_Time = 0;
        /// <summary>
        /// 天车下降时间
        /// </summary>
        public static short XP_Down_Time = 0;

        public static short LoadReadyFlagAddr = 2184;//0x888; 
        public static short FBLocation_W_FLAG = 2650;
        public static short FBLocation_W = 2660; 
        /// <summary>
        /// ?????????
        /// </summary>
        public static bool blnHaveProcessID = false;
         
        /// <summary>
        /// 是否有Fbar编号
        /// </summary>
        public static bool HaveFbarNumber = true; 
        /// <summary>
        /// 删除Pcode 站号地址　？？
        /// </summary>
        public static short intDeletePcodeStageNoAddr = 2181; 
        /// <summary>
        /// PLC内存缓存  2900
        /// </summary>
        public static short BufferDMAddr = 2900;  

        public static short TQL_timeOut = 0;
        public static short TQL_timeOut_forLevel1 = 0;
        public static short TQL_timeOut_forLevel2 = 0;
        public static short TQL_timeOut_FORLevel3 = 0;

        public static short intXPUnloadNo = 1;

        /// <summary>
        /// 告警系统单位内存数量，位数
        /// </summary>
        public static short AlarmSystemDMCount = 23;
        /// <summary>
        /// 天车程序4起始地址
        /// </summary>
        public static short XPProcRecAddr4; 

        #endregion 
        
        private static List<DMMapping> aDmMappingTable = new List<DMMapping>();
        private static Dictionary<string, string> aParameterSettingDic = new Dictionary<string, string>();

        #region 初始化

        /// <summary>
        /// 初始化参数Params & DMObject mapping & Controller & worker
        /// </summary>
        /// <returns></returns>
        /// [STAThread]
        public static bool Init()
        {
            bool aResult = false;
            try
            {
                //参数初始化
                //ou 隐藏
                //string indata = Strings.Format(Conversion.Val("1"), "0000") + "00010000" + Strings.Format(Conversion.Val("3"), "0000") + "0002";

                LoadTablesToCache();
                LoadPara();

                //初始化Controller，必须在ProcessDMObject之前，在LoadPara之后
                mController = new Controller();

                //加载并处理所有DM对象列表 
                ProcessDMObject(aDmMappingTable);
                if (isServer)
                {
                    //int num = Interaction.Shell(Application.StartupPath + @"\Backup.exe", AppWinStyle.NormalFocus, true, -1);//应该是备份数据库
                    //num = Interaction.Shell(Application.StartupPath + @"\InitDms.exe", AppWinStyle.NormalFocus, true, -1); 
                }

                //short num2 = (short) Math.Round((double) (Conversion.Int((double) (((double) NumStage) / ((double) NumXP))) + 10.0));
                short num2 = (short)Math.Round((double)(Conversion.Int(TryGetParameterIntValue("NumStage") / TryGetParameterIntValue("NumXP")) + 10.0));

                //NumStage/NumXP 
                TQL_timeOut = (short)(num2 + ((short)(XP_UP_Time + XP_Down_Time)));
                TQL_timeOut_FORLevel3 = (short)(((num2 / 2) + ((short)(XP_UP_Time + XP_Down_Time))) + 5);
                TQL_timeOut_forLevel2 = (short)(((num2 / 4) + ((short)(XP_UP_Time + XP_Down_Time))) + 2);
                TQL_timeOut_forLevel1 = 14;


                //创建 控制器实例
                if ((timerPeriod > 800) & isServer)
                {
                    mController._TimerState.tmr.Change(3000, timerPeriod); //0xbb8=3000
                }

                mController.Start(new Worker()); 
                aResult = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception objException = exception1;
                ProjectData.ClearProjectError();
            }

            return aResult;
        }
        #endregion 

        #region 加载数据表
        /// <summary>
        /// 从数据库中加载需要的表到内存中
        /// </summary>
        private static void LoadTablesToCache()
        {
            //加载参数表 
            DataSet dsParameters = AccessDBHelper.GetDataSetByTableName(new ParameterSetting().TableName);  
            foreach (DataRow dr in dsParameters.Tables[0].Rows)
            {
                if (dr["ParameterName"] != null && !string.IsNullOrWhiteSpace(dr["ParameterName"].ToString()))
                {
                    aParameterSettingDic[dr["ParameterName"].ToString()] = dr["ParameterValue"] == null ? string.Empty : dr["ParameterValue"].ToString();
                }
            }
            
            //加载DM table
            CommonHelper<DMMapping> dmHelper = new CommonHelper<DMMapping>();
            DataSet dsDmMappings = AccessDBHelper.GetDataSetByTableName(new DMMapping().TableName);
            aDmMappingTable = dmHelper.FillModelList(dsDmMappings.Tables[0]);
            
            //To do: 加载其他表
        }
        #endregion 

        #region 加载参数
        /// <summary>
        /// 加载所有参数
        /// </summary>
        private static void LoadPara()
        {
            NumBoard = 12;
            NumLot = 12;
            NumXPPrg = 5;
            NumXPStep = 50; 

            DMRunCycle = 8;
             
            IsNewPrg = 0;

            UsrStartAdd = 3100;

            ProgStart = 3101;
            ProgramCycleTimeAddr = 2390;
            PcodeGenByDMS = 1;
            initAddr = 2000;

            timerPeriod = -1;
            blnHaveProcessID = true;
            HaveFbarNumber = true;
            isServer = true; 
            intDeletePcodeStageNoAddr = 2710;
            LoadReadyFlagAddr = 2711; 
            BufferDMAddr = 2900; 

            FBLocation_W_FLAG = 2650;
            FBLocation_W = 2660;  
            XP_UP_Time = 10;
            XP_Down_Time = 10;
            TIN_1000 = 700;
            NI_1000 = 0;
            CU_1000 = 0;
            NI_2000 = 0;
            CU_2000 = 0;
            CU_3000 = 0;
            CU_4000 = 0;
            NI_4000 = 0;
             
        }

        /// <summary>
        /// 通过参数名称，获取参数列表中指定参数，并将参数转换成Int型
        /// </summary>
        /// <param name="theParamName"></param>
        /// <returns></returns>
        public static int TryGetParameterIntValue(string theParamName)
        {
            if (!aParameterSettingDic.ContainsKey(theParamName))
            {
                return 0;
            }

            string originalValue = aParameterSettingDic[theParamName];
            int aTranslatedValue = 0;
            int.TryParse(originalValue, out aTranslatedValue);
            return aTranslatedValue;
        }

        private static string splitChar = ",";
        /// <summary>
        /// 通过参数名称，获取参数列表中指定参数，并将按照Index获取以逗号分隔开的字符串，最后将该值转换成Int型
        /// </summary>
        /// <param name="theParamName"></param>
        /// <param name="theIndex"></param>
        /// <returns></returns>
        public static int TryGetParameterIntValue(string theParamName, int theIndex)
        {
            try
            {
                if (!aParameterSettingDic.ContainsKey(theParamName))
                {
                    return 0;
                }

                string originalValue = aParameterSettingDic[theParamName];
                if (string.IsNullOrWhiteSpace(originalValue))
                {
                    return 0;
                }

                string[] aStrs = originalValue.Split(new string[] { splitChar }, StringSplitOptions.RemoveEmptyEntries);
                string aTempStr = string.Empty;
                if (aStrs.Length > 0 && theIndex <= aStrs.Length)
                {
                    aTempStr = aStrs[theIndex];
                }
                else
                {
                    return 0;
                }

                if (string.IsNullOrWhiteSpace(aTempStr))
                {
                    return 0;
                }

                int aTranslatedValue = 0;
                int.TryParse(aTempStr, out aTranslatedValue);
                return aTranslatedValue;
            }
            catch (Exception ex)
            {

            }

            return 0;
        }

        #endregion 

        #region 处理DM Mapping
        /// <summary>
        /// 加载并处理所有DM对象列表
        /// </summary>
        private static void ProcessDMObject(List<DMMapping> theDmMapings)
        {
            DmClass aTempDm = null;
            List<DmClass> allTranslatedDMs = new List<DmClass>();
            foreach (var aDmMapping in theDmMapings)
            {
                aTempDm = new DM.DmClass();
                aTempDm.DMObjectIndexName = aDmMapping.DmIndex;
                aTempDm.CondiIndex = (short)aDmMapping.CondiIndex;
                aTempDm.DMAddressName = aDmMapping.DmName;
                aTempDm.DmAddress = aDmMapping.DmAddress;
                aTempDm.DmCount = (short)aDmMapping.DmCount;
                aTempDm.DmNodeType = (aDmMapping.DmNodeType == "1") ? DmNodeType.Parent : DmNodeType.Child;
                aTempDm.DmRelationID = aDmMapping.RelationID;
                aTempDm.DmRwType = aDmMapping.DmRWType == "1" ? DmRWType.Read : DmRWType.Write;
                aTempDm.DMObjectComment = aDmMapping.Comment;

                #region 根据DM的Index进行其他属性的赋值
                switch (aDmMapping.DmIndex)
                {
                    case "intXP_Display":
                        {
                            aTempDm.DmAddress = TryGetParameterIntValue("TransporterBlockAddr");
                            aTempDm.DmCount = (short)(TryGetParameterIntValue("TransporterBlockSize") * TryGetParameterIntValue("NumXP"));
                            aTempDm.IsCondiObject = false;
                            aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshXPCurLoc);
                            aTempDm.DmRunEventProcessThreadStatus = true;
                            aTempDm.DmStatus = true;

                            //这里意思是哪个Form用了这个天车。
                            aTempDm.AddFormNameToDmObjectWhereUsedList("frmPlantStatus");
                            aTempDm.AddFormNameToDmObjectWhereUsedList("frmimage");
                        }
                        break;
                    case "intLoadStageNo":
                        aTempDm.DmAddress = TryGetParameterIntValue("LoadStageNoAddr");
                        aTempDm.DmStatus = true;
                        break;
                    case "intXPStatus_R":
                        aTempDm.DmAddress = TryGetParameterIntValue("TransporterBlockAddr");
                        aTempDm.DmCount = (short)TryGetParameterIntValue("StageNoMaxLength");
                        aTempDm.IsCondiObject = true;
                        aTempDm.DmStatus = true;
                        aTempDm.DmObjectCondiProcessMethod = new DmCondiProcess(mController.ProcessCondi);
                        break;
                    case "intLoadBuffer_R":
                        var aLoadBufferAddr0 = TryGetParameterIntValue("loadBufferAddr", 0);
                        aTempDm.DmAddress = aLoadBufferAddr0;
                        aTempDm.DmCount = (short)TryGetParameterIntValue("LoadBufferCount"); 
                        aTempDm.AddFormNameToDmObjectWhereUsedList("frmPlantStatus");
                        aTempDm.DMObjectComment = "loadBufferAddr[0][" + aLoadBufferAddr0 + "]";
                        break;
                    case "intPcode_W":
                        var aLoadBufferPcodeAddr0 = TryGetParameterIntValue("LoadBufferPcodeAddr", 0);
                        if (PcodeGenByDMS == 1)
                        {
                            aTempDm.DmAddress = aLoadBufferPcodeAddr0;
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                        } 
                        if (isServer)
                        {
                            aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.GenNewPcode);
                        }
                        aTempDm.DMObjectComment = "PcodeGenByDMS == 1 : LoadBufferPcodeAddr[0][" + aLoadBufferPcodeAddr0 + "] ,else: -1 ";
                        break;
                    case "intLoadBuffer_R2":
                        var aLoadBufferAddr1 = TryGetParameterIntValue("loadBufferAddr", 1);
                        aTempDm.DmAddress = aLoadBufferAddr1;
                        aTempDm.DmCount = (short)TryGetParameterIntValue("LoadBufferCount"); 
                        aTempDm.AddFormNameToDmObjectWhereUsedList("frmPlantStatus");
                        aTempDm.DMObjectComment = "loadBufferAddr[1][" + aLoadBufferAddr1 + "]";
                        break;
                    case "intPcode_W2":
                        var aLoadBufferPcodeAddr1 = TryGetParameterIntValue("LoadBufferPcodeAddr", 1);
                        if (PcodeGenByDMS == 1)
                        {
                            aTempDm.DmAddress = aLoadBufferPcodeAddr1;
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                        } 

                        if (isServer)
                        {
                            aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.GenNewPcode);
                        }
                        aTempDm.DMObjectComment = "PcodeGenByDMS == 1 : LoadBufferPcodeAddr[1][" + aLoadBufferPcodeAddr1 + "] ,else: -1 ";
                        break;
                    case "intLoadBuffer_R3":
                        var aloadBufferAddr2 = TryGetParameterIntValue("loadBufferAddr", 2);
                        aTempDm.DmAddress = aloadBufferAddr2;
                        aTempDm.DmCount = (short)TryGetParameterIntValue("LoadBufferCount");
                        aTempDm.AddFormNameToDmObjectWhereUsedList("frmPlantStatus");
                        aTempDm.DMObjectComment = "loadBufferAddr[2][" + aloadBufferAddr2 + "]";
                        break;
                    case "intPcode_W3":
                        var aLoadBufferPcodeAddr2 = TryGetParameterIntValue("LoadBufferPcodeAddr", 2);
                        if (PcodeGenByDMS == 1)
                        {
                            aTempDm.DmAddress = aLoadBufferPcodeAddr2;
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                        }
                         
                        if (isServer)
                        {
                            aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.GenNewPcode);
                        }
                        aTempDm.DMObjectComment = "PcodeGenByDMS == 1 : LoadBufferPcodeAddr[2][" + aLoadBufferPcodeAddr2 + "] ,else :-1";
                        break;
                    case "intLoadBuffer_R4":
                        var aloadBufferAddr3 = TryGetParameterIntValue("loadBufferAddr", 3);
                        aTempDm.DmAddress = aloadBufferAddr3;
                        aTempDm.DmCount = (short)TryGetParameterIntValue("LoadBufferCount"); 
                        aTempDm.AddFormNameToDmObjectWhereUsedList("frmPlantStatus");
                        aTempDm.DMObjectComment = "loadBufferAddr[3][" + aloadBufferAddr3 + "]";
                        break;
                    case "intPcode_W4":
                        var aLoadBufferPcodeAddr3 = TryGetParameterIntValue("LoadBufferPcodeAddr", 3);
                        if (PcodeGenByDMS == 1)
                        {
                            aTempDm.DmAddress = aLoadBufferPcodeAddr3;
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                        } 

                        if (isServer)
                        {
                            aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.GenNewPcode);
                        }
                        aTempDm.DMObjectComment = "PcodeGenByDMS == 1 : LoadBufferPcodeAddr[3][" + aLoadBufferPcodeAddr3 + "] ,else : -1";
                        break;
                    case "intXPStatus_W":
                        if (isServer)
                        {
                            aTempDm.DmAddress = (GetDmAddress(allTranslatedDMs, "intXPStatus_R") + TryGetParameterIntValue("StageNoMaxLength")) - 1;
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                        }
                        aTempDm.DmContent = "0"; 
                        aTempDm.DMObjectComment = "server: (mController.GetDmObject(intXPStatus_R).DmAddress + StageNoMaxLength) - 1 ,esle: -1";
                        break;
                    case "intLoadStageNo_W":
                        aTempDm.DmAddress = TryGetParameterIntValue("LoadStageNoAddr"); 
                        aTempDm.DmContent = "0000";
                        break;
                    case "intRectAlarm_W":
                        aTempDm.DmAddress = 2088; 
                        aTempDm.DmContent = "0000";
                        break;
                    case "intUnloadingFlag_W":
                        aTempDm.DmAddress = TryGetParameterIntValue("unloadingFlag_addr"); 
                        aTempDm.DmContent = "0000";
                        break;
                    case "intwscs_W":
                        aTempDm.DmAddress = 2712; 
                        aTempDm.DmContent = "0001";
                        break;
                    case "intXPUnloadStatus_R":
                        var aTransporterBlockAddr = TryGetParameterIntValue("TransporterBlockAddr");
                        var aTransporterBlockSize = TryGetParameterIntValue("TransporterBlockSize");
                        var aintXPUnloadNo = TryGetParameterIntValue("intXPUnloadNo");
                        aTempDm.DmAddress = aTransporterBlockAddr + (aTransporterBlockSize * (aintXPUnloadNo - 1));
                        aTempDm.DmCount = (short)TryGetParameterIntValue("StageNoMaxLength");
                        aTempDm.IsCondiObject = true;
                        aTempDm.DmObjectCondiProcessMethod = new DmCondiProcess(mController.ProcessCondi);
                        aTempDm.DMObjectComment = "TransporterBlockAddr[" + aTransporterBlockAddr + "] + (TransporterBlockSize[" + aTransporterBlockSize + "] * (intXPUnloadNo[" + aintXPUnloadNo + "] - 1))";
                        break;
                    case "intUnloadArea_R":
                        aTempDm.DmAddress = TryGetParameterIntValue("UnloadPcodeAddr");
                        if (timerPeriod > 800)
                        {
                            aTempDm.DmCount = 1;
                        }
                        else
                        {
                            aTempDm.DmCount = (short)(1 + ((short)(((short)(TryGetParameterIntValue("NumFbar") * TryGetParameterIntValue("NumChem"))) * TryGetParameterIntValue("numSide"))));
                        }

                        if (isServer)
                        {
                            aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.SaveUnloadTodb);
                        }

                        DmClass aRelatedDm = null;
                        if (intXPUnloadNo == 1)
                        {
                            aTempDm.CondiIndex = 1;
                            aRelatedDm = GetDm(allTranslatedDMs, "intXPStatus_W");
                            if (aRelatedDm != null)
                            {
                                aRelatedDm.DmRelationID = aDmMapping.DmIndex;
                            }
                        }
                        else
                        {
                            aRelatedDm = GetDm(allTranslatedDMs, "intXPUnloadStatus_R");
                            if (aRelatedDm != null)
                            {
                                aRelatedDm.DmRelationID = aDmMapping.DmIndex;
                            }
                        }
                        break;
                    case "intXPUnloadReset": 
                        aTempDm.DmContent = "1"; 
                        if (intXPUnloadNo == 1)
                        {
                            aTempDm.CondiIndex = 1;
                            if (isServer)
                            {
                                aTempDm.DmAddress = (GetDmAddress(allTranslatedDMs, "intXPStatus_R") + TryGetParameterIntValue("StageNoMaxLength")) - 1;
                            }
                            else
                            {
                                aTempDm.DmAddress = -1;
                            }
                        }
                        else
                        {
                            if (isServer)
                            {
                                aTempDm.DmAddress = (GetDmAddress(allTranslatedDMs, "intXPUnloadStatus_R") + TryGetParameterIntValue("StageNoMaxLength")) - 1;
                            }
                            else
                            {
                                aTempDm.DmAddress = -1;
                            }
                        }
                        break;
                    case "intUnloadPcode_W":
                        if (isServer)
                        {
                            aTempDm.DmAddress = TryGetParameterIntValue("UnloadPcodeAddr");
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                        } 
                        aTempDm.DmContent = "0000"; 
                        if (intXPUnloadNo == 1)
                        {
                            aTempDm.CondiIndex = 1;
                        }

                        break;
                    case "intLoadTimeInterval":
                        if (isServer)
                        {
                            aTempDm.DmAddress = TryGetParameterIntValue("LoadTimeIntervalAddr");
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                        } 
                        aTempDm.DmContent = "00000000";
                        break;
                    case "intPcodeOnXP":
                        aTempDm.DmAddress = TryGetParameterIntValue("PcodeOnXPAddr");
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumXP");
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshPcodeOnXPArry);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        aTempDm.DmStatus = true;
                        break;
                    case "intXPCMM_F":
                        aTempDm.DmAddress = TryGetParameterIntValue("NewXPCommandFlagAddr");
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumXP");;
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshNew_CMM_F_Arry);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        aTempDm.DmStatus = true;
                        break;
                    case "intAlarm_pcode_fb_miss":
                        aTempDm.DmAddress = -1; 
                        aTempDm.DmStatus = true;
                        aTempDm.AddFormNameToDmObjectWhereUsedList("FrmImage");
                        break;
                    case "intPcodeArea":
                        aTempDm.DmAddress = TryGetParameterIntValue("PcodeAreaAddr");
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumPcode");
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshPcodeArry);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        aTempDm.DmStatus = true;
                        aTempDm.AddFormNameToDmObjectWhereUsedList("frmPlantStatus");
                        aTempDm.AddFormNameToDmObjectWhereUsedList("frmimage");
                        break;
                    case "intFBArea":
                        aTempDm.DmAddress = -1;
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumPcode");
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshFBArry);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        break;
                    case "intLoading":
                        aTempDm.DmAddress = 2930; 
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshLoading);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        aTempDm.DmStatus = true;
                        break;
                    case "intPresetPTImmT":
                        if (isServer)
                        {
                            aTempDm.DmAddress = TryGetParameterIntValue("PresetPTImmTAddr");
                            if (TryGetParameterIntValue("PresetPTImmTAddr") > 0)
                            {
                                aTempDm.init_elem(mController.intHaveRectStageS);
                            }
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                        }
                        aTempDm.DmCount = (short)(mController.intNumRect_ForPreset * 2);
                        break;
                    case "intPresetAMP":
                        if (isServer)
                        {
                            aTempDm.DmAddress = TryGetParameterIntValue("PresetAmpAddr");
                            if (TryGetParameterIntValue("PresetAmpAddr") > 0)
                            {
                                aTempDm.init_elem(mController.intHaveRectStageS);
                            }
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                        }
                        aTempDm.DmCount = mController.intNumRect_ForPreset;
                        break;
                    case "intstg_step":
                        var aCurStgStepDmAddr = TryGetParameterIntValue("cur_stg_step_DM_ADDR");
                        var aNumStage = (short)TryGetParameterIntValue("NumStage");
                        if (isServer)
                        {
                            aTempDm.DmAddress = aCurStgStepDmAddr;
                            if (aCurStgStepDmAddr > 0)
                            {
                                aTempDm.init_elem(aNumStage);
                            }
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                        }
                        aTempDm.DmCount = aNumStage;
                        break;
                    case "intImmTime":
                        aTempDm.DmAddress = TryGetParameterIntValue("ImmersionTimeAddr");
                        aTempDm.DmCount = GetDmCount(allTranslatedDMs, "intPcodeArea");   
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshImmTimeArry);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        aTempDm.DmStatus = true;
                        break;
                    case "intEloadBuffer_W":
                        aTempDm.DmAddress = TryGetParameterIntValue("eloadBufferAddr");
                        aTempDm.DmCount = (short)(TryGetParameterIntValue("LoadBufferCount") - 1);
                        aTempDm.IsCondiObject = true;
                        aTempDm.DmObjectCondiProcessMethod = new DmCondiProcess(mController.ProcessCondi);
                        break;
                    case "intEloadActionFlag_W":
                        if (PcodeGenByDMS == 1)
                        {
                            aTempDm.DmAddress = -1;
                        }
                        else
                        {
                            aTempDm.DmAddress = TryGetParameterIntValue("eloadActionFlagAddr");
                        } 
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.GenEloadActionFlag); 
                        break;
                    case "intEloadPcode":
                        if (PcodeGenByDMS == 1)
                        {
                            aTempDm.DmAddress = TryGetParameterIntValue("eloadBufferPcodeAddr");
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                        }
                         
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.GenEloadPcode); 
                        break;
                    case "intEloadStage":
                        aTempDm.DmAddress = GetDmAddress(allTranslatedDMs, "intEloadBuffer_W") - 1; 
                        if (PcodeGenByDMS == 1)
                        {
                            aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.SaveEloadTodb);
                        } 
                        break;
                    case "intEloadStage_R":
                        if (PcodeGenByDMS == 1)
                        {
                            aTempDm.DmAddress = -1;
                        }
                        else
                        {
                            aTempDm.DmAddress = GetDmAddress(allTranslatedDMs, "intEloadBuffer_W") - 1;
                        } 
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshReadEloadStageNo); 
                        break;
                    case "intEloadPcode_R":
                        if (PcodeGenByDMS == 1)
                        {
                            aTempDm.DmAddress = -1;
                        }
                        else
                        {
                            aTempDm.DmAddress = TryGetParameterIntValue("eloadBufferPcodeAddr");
                        } 
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.SaveEloadTodb);
                        break;

                    case "intFbarLocation_W":
                        aTempDm.DmAddress = TryGetParameterIntValue("intFbarCurLocationAddr");
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumStage");
                        if (aTempDm.DmAddress > 0)
                        {
                            aTempDm.init_elem(aTempDm.DmCount);
                        }
                        break;
                    case "intPresetTemp_W":
                        var aPresetTempAddr = TryGetParameterIntValue("presetTempAddr");
                        var aNumTemperature = (short)TryGetParameterIntValue("NumTemperature");
                        aTempDm.DmAddress = aPresetTempAddr;
                        aTempDm.DmCount = aNumTemperature;
                        if (aPresetTempAddr > 0)
                        {
                            aTempDm.init_elem(aNumTemperature);
                        }
                        break;
                    case "intPresetTemp2_W":
                        aTempDm.DmAddress = TryGetParameterIntValue("presetTemp2Addr");
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumTemperature");
                        if (aTempDm.DmAddress > 0)
                        {
                            aTempDm.init_elem(aTempDm.DmCount);
                        }
                        break;
                    case "intPresetTemp1_W":
                        aTempDm.DmAddress = 2800;
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumTemperature");
                        if (aTempDm.DmAddress > 0)
                        {
                            aTempDm.init_elem(aTempDm.DmCount);
                        }
                        break;
                    case "intActAMP":
                        aTempDm.DmAddress = TryGetParameterIntValue("ActualAmpAddr");
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumRect");
                        aTempDm.DmStatus = true;
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshAMPArry);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        aTempDm.ObjRunIndex = 1;
                        break;

                    case "intAlarmSystem":
                        aTempDm.DmAddress = TryGetParameterIntValue("AlarmSystem_Addr");
                        aTempDm.DmCount = (short)TryGetParameterIntValue("AlarmSystemDMCount");
                        aTempDm.DmStatus = true;

                        aTempDm.AddFormNameToDmObjectWhereUsedList("FrmAlarmSystem");
                        break;
                    case "intActualTemp_R":
                        aTempDm.DmAddress = TryGetParameterIntValue("ActualTempAddr");
                        aTempDm.DmCount =  GetDmCount(allTranslatedDMs, "intPresetTemp_W");  
                        aTempDm.DmStatus = true;
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshTemperatureArry);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        aTempDm.ObjRunIndex = 4;
                        aTempDm.AddFormNameToDmObjectWhereUsedList("TemperatureSettingFrm");
                        break;
                    case "intPresetDAH_W":
                        var aNumDAH = (short)TryGetParameterIntValue("NumDAH");
                        aTempDm.DmAddress = TryGetParameterIntValue("PresetDahAddr");
                        aTempDm.DmCount = aNumDAH;
                        if (aTempDm.DmAddress > 0)
                        {
                            aTempDm.init_elem(aNumDAH);
                        }
                        break;
                    case "intPresetDT_W":
                        var aNumDAH1 = (short)TryGetParameterIntValue("NumDAH");
                        aTempDm.DmAddress = TryGetParameterIntValue("PresetDTAddr");
                        aTempDm.DmCount = aNumDAH1;
                        if (aTempDm.DmAddress > 0)
                        {
                            aTempDm.init_elem(aNumDAH1);
                        }
                        break;
                    case "intActualDAH_R":
                        aTempDm.DmAddress = TryGetParameterIntValue("ActualDahAddr");
                        aTempDm.DmCount = GetDmCount(allTranslatedDMs, "intPresetDAH_W"); 
                        aTempDm.DmStatus = true;
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshACTDAHArry);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        aTempDm.ObjRunIndex = 3;
                        aTempDm.AddFormNameToDmObjectWhereUsedList("frmDosingDAH");
                        break;
                    case "intActualDAH_R1":
                        aTempDm.DmAddress = TryGetParameterIntValue("sycs_dosing_addr");
                        aTempDm.DmCount = GetDmCount(allTranslatedDMs, "intPresetDAH_W");
                        aTempDm.DmStatus = true;
                        aTempDm.AddFormNameToDmObjectWhereUsedList("frmDosingDAH");
                        break;
                    case "intACCDAH_R":
                        aTempDm.DmAddress = TryGetParameterIntValue("ACCDahAddr");
                        aTempDm.DmCount = (short)(GetDmCount(allTranslatedDMs, "intPresetDAH_W") * 2);
                        aTempDm.DmStatus = true;
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshACCDAHArry);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        aTempDm.ObjRunIndex = 4;
                        aTempDm.AddFormNameToDmObjectWhereUsedList("frmDosingDAH");
                        break;
                    case "intResetACCDAH_R":
                        aTempDm.DmAddress = GetDmAddress(allTranslatedDMs, "intACCDAH_R");
                        aTempDm.DmCount = (short)(GetDmCount(allTranslatedDMs, "intPresetDAH_W") * 2);
                        if (aTempDm.DmAddress > 0)
                        {
                            aTempDm.init_elem((short)TryGetParameterIntValue("NumDAH"));
                        }
                        break;
                    case "intResetACCDAH_R1":
                        aTempDm.DmAddress = TryGetParameterIntValue("sycs_dosing_addr"); 
                        aTempDm.DmCount = (short)(GetDmCount(allTranslatedDMs, "intPresetDAH_W") * 1);
                        if (aTempDm.DmAddress > 0)
                        {
                            aTempDm.init_elem((short)TryGetParameterIntValue("NumDAH"));
                        }
                        break;
                    case "intDosingPumpStatus":
                        aTempDm.DmAddress = TryGetParameterIntValue("DosingPumpAddr");
                        aTempDm.DmCount = (short)Math.Round(Conversion.Int((double)(((double)(TryGetParameterIntValue("NumDAH") + 15)) / 16.0)));
                        aTempDm.DmStatus = true;
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshDosDahPumpStatusArry);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        aTempDm.AddFormNameToDmObjectWhereUsedList("frmDosingDAH");
                        break;
                    case "intPresetCM":
                        var aNumCMDosing = (short)TryGetParameterIntValue("NumCMDosing");
                        aTempDm.DmAddress = TryGetParameterIntValue("PresetCMAddr");
                        aTempDm.DmCount = (short)(aNumCMDosing * 2);
                        if (aNumCMDosing > 0)
                        {
                            aTempDm.init_elem(aNumCMDosing);
                        }
                        break;
                    case "intPresetCMDT":
                        aTempDm.DmAddress = TryGetParameterIntValue("PresetCMDTAddr"); 
                        aTempDm.DmCount = (short)Math.Round((double)((double)GetDmCount(allTranslatedDMs, "intPresetCM") / 2.0));
                        if (aTempDm.DmCount > 0)
                        {
                            aTempDm.init_elem(aTempDm.DmCount);
                        }
                        break;
                    case "intCMDosingPump":
                        aTempDm.DmAddress = TryGetParameterIntValue("CmDosingPumpAddr");
                        aTempDm.DmCount = (short)Math.Round(Conversion.Int((double)(((double)(TryGetParameterIntValue("NumCMDosing") + 15)) / 16.0)));
                        aTempDm.DmStatus = true;
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshCMDosingPumpArry);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        aTempDm.AddFormNameToDmObjectWhereUsedList("FrmCM");
                        break;
                    case "intActCM":
                        aTempDm.DmAddress = TryGetParameterIntValue("ActualCMAddr");
                        aTempDm.DmCount = GetDmCount(allTranslatedDMs, "intPresetCM");   
                        aTempDm.DmStatus = true;
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshActCMArry);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        aTempDm.ObjRunIndex = 3;
                        aTempDm.AddFormNameToDmObjectWhereUsedList("FrmCM");
                        break;
                    case "intACCCM":
                        aTempDm.DmAddress = TryGetParameterIntValue("ACCCMAddr");
                        aTempDm.DmCount = (short)(TryGetParameterIntValue("NumACCCM") * 2);
                        aTempDm.DmStatus = true;
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshACCCMArry);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        aTempDm.ObjRunIndex = 2;
                        aTempDm.AddFormNameToDmObjectWhereUsedList("FrmCM");
                        break;
                    case "intResetACCCM":
                        var aNumAccCm = (short)TryGetParameterIntValue("NumACCCM");
                        aTempDm.DmAddress = GetDmAddress(allTranslatedDMs, "intACCCM");
                        aTempDm.DmCount = (short)(aNumAccCm * 2);
                        if (aTempDm.DmAddress > 0)
                        {
                            aTempDm.init_elem(aNumAccCm);
                        }
                        break;
                    case "intLoadReadyStageNo":
                        aTempDm.DmAddress = TryGetParameterIntValue("LoadReadyStageNoAddr");
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumLoadCount");
                        if (aTempDm.DmAddress > 0)
                        {
                            aTempDm.init_elem(aTempDm.DmCount);
                        }
                        break;
                    case "intLoadReadyFlag":
                        aTempDm.DmAddress = LoadReadyFlagAddr;
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumLoadCount");
                        aTempDm.DmContent = "0001";
                        if (aTempDm.DmAddress > 0)
                        {
                            aTempDm.init_elem(aTempDm.DmCount);
                        }
                        break;
                    case "intLoadReadyFlag_R":
                        aTempDm.DmAddress = LoadReadyFlagAddr;
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumLoadCount");
                        aTempDm.AddFormNameToDmObjectWhereUsedList("FrmInput");
                        break;
                    case "intLoadBuffer_W":
                        aTempDm.DmAddress = GetDmAddress(allTranslatedDMs, "intLoadBuffer_R"); 
                        aTempDm.DmCount = GetDmCount(allTranslatedDMs, "intLoadBuffer_R");     
                        break;
                    case "intLoadBuffer_W2":
                        aTempDm.DmAddress = GetDmAddress(allTranslatedDMs, "intLoadBuffer_R2");  
                        aTempDm.DmCount = GetDmCount(allTranslatedDMs, "intLoadBuffer_R2");    
                        aTempDm.AddFormNameToDmObjectWhereUsedList("frmPlantStatus");
                        break;
                    case "intLoadBuffer_W3":
                        aTempDm.DmAddress = GetDmAddress(allTranslatedDMs, "intLoadBuffer_R3");    
                        aTempDm.DmCount = GetDmCount(allTranslatedDMs, "intLoadBuffer_R3");    
                        aTempDm.AddFormNameToDmObjectWhereUsedList("frmPlantStatus");
                        break;
                    case "intLoadBuffer_W4":
                        aTempDm.DmAddress = GetDmAddress(allTranslatedDMs, "intLoadBuffer_R4");    
                        aTempDm.DmCount = GetDmCount(allTranslatedDMs, "intLoadBuffer_R4");   
                        aTempDm.AddFormNameToDmObjectWhereUsedList("frmPlantStatus");
                        break;
                    case "intSystemMode":
                        aTempDm.DmAddress = TryGetParameterIntValue("systemModeAddr"); 
                        aTempDm.DmStatus = true;
                        aTempDm.AddFormNameToDmObjectWhereUsedList("frmPlantStatus");
                        break;
                    case "intActVolt":
                        aTempDm.DmAddress = TryGetParameterIntValue("ActualVoltAddr");
                        aTempDm.DmCount = GetDmCount(allTranslatedDMs, "intActAMP");   
                        aTempDm.DmStatus = true;
                        aTempDm.ObjRunIndex = 2;
                        aTempDm.AddFormNameToDmObjectWhereUsedList("FrmRect");
                        break;
                    case "intBPTOn_W":
                        aTempDm.DmAddress = TryGetParameterIntValue("BPTOnAddr");
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumBPT");
                        if (aTempDm.DmAddress > 0)
                        {
                            aTempDm.init_elem(aTempDm.DmCount);
                        }
                        break;
                    case "intBPTOFF_W":
                        aTempDm.DmAddress = TryGetParameterIntValue("BPTOffAddr");
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumBPT");
                        if (aTempDm.DmAddress > 0)
                        {
                            aTempDm.init_elem(aTempDm.DmCount);
                        }
                        break;
                    case "intBPTTotal_W":
                        aTempDm.DmAddress = TryGetParameterIntValue("BPTTotalAddr");
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumBPT");
                        if (aTempDm.DmAddress > 0)
                        {
                            aTempDm.init_elem(aTempDm.DmCount);
                        }
                        break;
                    case "intVBTOn_W":
                        aTempDm.DmAddress = TryGetParameterIntValue("VBTOnAddr");
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumVBT");
                        if (aTempDm.DmAddress > 0)
                        {
                            aTempDm.init_elem(aTempDm.DmCount);
                        }
                        break;
                    case "intVBTOFF_W":
                        aTempDm.DmAddress = TryGetParameterIntValue("VBTOffAddr");
                        aTempDm.DmCount = GetDmCount(allTranslatedDMs, "intVBTOn_W");   
                        if (aTempDm.DmAddress > 0)
                        {
                            aTempDm.init_elem((short)TryGetParameterIntValue("NumVBT"));
                        }
                        break;
                    case "intVBTTotal_W":
                        aTempDm.DmAddress = TryGetParameterIntValue("VBTTotalAddr");
                        aTempDm.DmCount = GetDmCount(allTranslatedDMs, "intVBTOn_W");  
                        if (aTempDm.DmAddress > 0)
                        {
                            aTempDm.init_elem((short)TryGetParameterIntValue("NumVBT"));
                        }
                        break;

                    case "intCurCycleTime_R":
                        aTempDm.DmAddress = TryGetParameterIntValue("CurPrgCT"); 
                        aTempDm.DmStatus = true;
                        aTempDm.AddFormNameToDmObjectWhereUsedList("frmPlantStatus");
                        break;
                    case "intRectStatus":
                        aTempDm.DmAddress = TryGetParameterIntValue("RectModeAddr");
                        var dmCount = GetDmCount(allTranslatedDMs, "intActAMP");
                        aTempDm.DmCount = (short)Math.Round(Conversion.Int((double)(((double)((dmCount * 2) + 15)) / 16.0)));
                        aTempDm.DmStatus = true;
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshRectStatusArry);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        break;
                    case "intPTime_W":
                        aTempDm.DmAddress = TryGetParameterIntValue("PresetPTAddr");
                        aTempDm.DmCount = (short)(TryGetParameterIntValue("NumChem") * 2);
                        break;
                    case "intTempErrorFlag_Set":
                        if (isServer)
                        {
                            aTempDm.DmAddress = TryGetParameterIntValue("TempFlagAddr");
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                        }
                        aTempDm.init_elem((short)TryGetParameterIntValue("NumTemperature"));
                        break;
                    case "intTempErrorFlag_Reset":
                        if (isServer)
                        {
                            aTempDm.DmAddress = GetDmAddress(allTranslatedDMs, "intTempErrorFlag_Set");  
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                        }
                        aTempDm.DmCount = GetDmCount(allTranslatedDMs, "intTempErrorFlag_Set");  
                        aTempDm.init_elem((short)TryGetParameterIntValue("NumTemperature"));
                        break;
                    case "intPTexpiry":
                        if (timerPeriod <= 200)
                        {
                            aTempDm.DmAddress = TryGetParameterIntValue("PtExpiryFlagAddr");
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                        }
                        var aNumRect = TryGetParameterIntValue("NumRect");
                        aTempDm.DmCount = (short)Math.Round(Conversion.Int((double)((((double)(aNumRect - 1)) / 16.0) + 1.0)));
                        aTempDm.DmStatus = true;
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshRectPTexpiryArry);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        break;
                    case "intXPStep":
                        aTempDm.DmAddress = TryGetParameterIntValue("XPStepAddr");
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumXP");
                        aTempDm.DmStatus = true;
                        aTempDm.AddFormNameToDmObjectWhereUsedList("FrmXPStep");
                        break;
                    case "intSwitch1_W":
                        aTempDm.DmAddress = TryGetParameterIntValue("SwitchAddr1");
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumSwitch1");
                        if (aTempDm.DmAddress > 0)
                        {
                            aTempDm.init_elem(aTempDm.DmCount);
                        }
                        break;
                    case "intSwitch1_r":
                        aTempDm.DmAddress = TryGetParameterIntValue("SwitchAddr1");
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumSwitch1");
                        aTempDm.AddFormNameToDmObjectWhereUsedList("FrmSwitch1");
                        break;
                    case "intInto1Step":
                        aTempDm.DmAddress = 2970; 
                        aTempDm.DmStatus = true;
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshReadCur_step_into_1_stage);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        break;
                    case "intSwitch1_rr":
                        aTempDm.DmAddress = 2955; 
                        aTempDm.DmStatus = true;
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshSwitchStatus);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        break;
                    case "intInto1Step_RESET":
                        aTempDm.DmAddress = GetDmAddress(allTranslatedDMs, "intInto1Step");   
                        aTempDm.DmContent = "0000";
                        break;
                    case "intHW_FLAG_RESET":
                        aTempDm.DmAddress = GetDmAddress(allTranslatedDMs, "intSwitch1_rr");  
                        aTempDm.DmContent = "0000";
                        break;
                    case "intSwitch2_W":
                        aTempDm.DmAddress = TryGetParameterIntValue("SwitchAddr2");
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumSwitch2");
                        if (aTempDm.DmAddress > 0)
                        {
                            aTempDm.init_elem(aTempDm.DmCount);
                        }
                        break;
                    case "intSwitch2_r":
                        aTempDm.DmAddress = TryGetParameterIntValue("SwitchAddr2");
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumSwitch2");
                        aTempDm.AddFormNameToDmObjectWhereUsedList("FrmSwitch2");
                        break;
                    case "intPTexpiryFlag_Set":
                        if (isServer)
                        {
                            aTempDm.DmAddress = TryGetParameterIntValue("Pt_expiryFlagAddr");
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                        }
                        aTempDm.init_elem((short)TryGetParameterIntValue("NumRect"));
                        break;
                    case "intPTexpiryFlag_Reset":
                        if (isServer)
                        {
                            aTempDm.DmAddress = TryGetParameterIntValue("Pt_expiryFlagAddr");
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                        }
                        aTempDm.init_elem((short)TryGetParameterIntValue("NumRect"));
                        break;
                    case "intFbarCurLocation_R":
                        aTempDm.DmAddress = TryGetParameterIntValue("intFbarCurLocationAddr");
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumStage");
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshFbarLocArry);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        aTempDm.ObjRunIndex = 1;
                        break;
                    case "intXPProcRec1":
                        if (isServer)
                        {
                            aTempDm.DmAddress = TryGetParameterIntValue("XPProcRecAddr", 0);//XPProcRecAddr1
                            aTempDm.DmCount = 8;
                            aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshXPProcRecArry);
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                            aTempDm.DmCount = -1;
                        }
                        aTempDm.DmStatus = true;
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        break;
                    case "intXPProcRec2":
                        if (isServer)
                        {
                            aTempDm.DmAddress = TryGetParameterIntValue("XPProcRecAddr", 1);//XPProcRecAddr2
                            aTempDm.DmCount = 8;
                            aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshXPProcRecArry);
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                            aTempDm.DmCount = -1;
                        }
                        aTempDm.DmStatus = true;
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        break;
                    case "intXPProcRec3":
                        if (isServer)
                        {
                            aTempDm.DmAddress = TryGetParameterIntValue("XPProcRecAddr", 2);//XPProcRecAddr3
                            aTempDm.DmCount = 8;
                            aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshXPProcRecArry);
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                            aTempDm.DmCount = -1;
                        }
                        aTempDm.DmStatus = true;
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        break;
                    case "intXPProcRec4":
                        if (isServer)
                        {
                            aTempDm.DmAddress = TryGetParameterIntValue("XPProcRecAddr", 3);//XPProcRecAddr4
                            aTempDm.DmCount = 8;
                            aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshXPProcRecArry);
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                            aTempDm.DmCount = -1;
                        }
                        aTempDm.DmStatus = true;
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        break;
                    case "intXPProcRecFlagReset1":
                        if (isServer)
                        {
                            aTempDm.DmAddress = GetDmAddress(allTranslatedDMs, "intXPProcRec1"); 
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                        } 
                        aTempDm.DmContent = "0000";
                        break;
                    case "intXPProcRecFlagReset2":
                        if (isServer)
                        {
                            aTempDm.DmAddress = GetDmAddress(allTranslatedDMs, "intXPProcRec2");
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                        } 
                        aTempDm.DmContent = "0000";
                        break;
                    case "intXPProcRecFlagReset3":
                        if (isServer)
                        {
                            aTempDm.DmAddress = GetDmAddress(allTranslatedDMs, "intXPProcRec3"); 
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                        } 
                        aTempDm.DmContent = "0000";
                        break;
                    case "intXPProcRecFlagReset4":
                        if (isServer)
                        {
                            aTempDm.DmAddress = GetDmAddress(allTranslatedDMs, "intXPProcRec4");
                        }
                        else
                        {
                            aTempDm.DmAddress = -1;
                        } 
                        aTempDm.DmContent = "0000";
                        break;

                    #region G18030 New Line
                    case "intXPCMM_s":
                        aTempDm.DmAddress = TryGetParameterIntValue("intXPCommandAddr");
                        aTempDm.DmCount = (short)(TryGetParameterIntValue("NumXP") * 5);
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshNew_CMM_s_Arry);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        aTempDm.DmStatus = true;
                        break;
                    case "intPcodeArea1":
                        aTempDm.DmAddress = 2130;
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshRECTCODE);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        aTempDm.DmStatus = true;
                        break;
                    case "intpuls_1":
                        aTempDm.DmAddress = TryGetParameterIntValue("puls_add");
                        break;
                    case "intpuls_2":
                        aTempDm.DmAddress = TryGetParameterIntValue("puls_add") + 28;
                        break;
                    case "intpuls_3":
                        aTempDm.DmAddress = TryGetParameterIntValue("puls_add") + 2 * 28;
                        break;
                    case "intpuls_4":
                        aTempDm.DmAddress = TryGetParameterIntValue("puls_add") + 3 * 28;
                        break;
                    case "intpuls_5":
                        aTempDm.DmAddress = TryGetParameterIntValue("puls_add") + 4 * 28;
                        break;
                    case "intpuls_6":
                        aTempDm.DmAddress = TryGetParameterIntValue("puls_add") + 5 * 28;
                        break;
                    case "intpuls1_1":
                    case "intpuls1_2":
                    case "intpuls1_3":
                    case "intpuls1_4":
                    case "intpuls1_5":
                    case "intpuls1_6":
                        aTempDm.DmAddress = -1;
                        break;
                    case "intPresetTemp_ll_W":
                        aTempDm.DmAddress = 2510;
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumTemperature");
                        aTempDm.init_elem(aTempDm.DmCount);
                        break;
                    case "intPresetTemp_hh_W":
                        aTempDm.DmAddress = 2480;
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumTemperature");
                        aTempDm.init_elem(aTempDm.DmCount);
                        break;
                    case "intPresetTemp_zz_W":
                        aTempDm.DmAddress = 2540;
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumTemperature");
                        aTempDm.init_elem(aTempDm.DmCount);
                        break;
                    case "intPresetTemp_W_f":
                        aTempDm.DmAddress = -1;
                        break;
                    case "intAct_AH":
                        aTempDm.DmAddress = 2300;
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumRect");
                        aTempDm.DmStatus = true;
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshACT_AH_Arry);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        aTempDm.ObjRunIndex = 1;
                        aTempDm.AddFormNameToDmObjectWhereUsedList("FrmRect");
                        break;
                    case "intPRE_AH":
                        aTempDm.DmAddress = 2230;
                        aTempDm.DmCount = (short)TryGetParameterIntValue("NumRect");
                        aTempDm.DmStatus = true;
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshPRE_AH_Arry);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        aTempDm.ObjRunIndex = 1;
                        break;
                    case "intACCDAH_R1":
                        aTempDm.DmAddress = 2590;
                        aTempDm.DmStatus = true;
                        aTempDm.DmObjectEventProcessMethod = new DmEventProcess(mController.RefreshACCDAHArry1);
                        aTempDm.DmRunEventProcessThreadStatus = true;
                        aTempDm.ObjRunIndex = 4;
                        aTempDm.AddFormNameToDmObjectWhereUsedList("frmDosingDAH");
                        break;
                    case "intACCCM1":
                        aTempDm.DmAddress = 2598;
                        aTempDm.DmCount = (short)(TryGetParameterIntValue("NumACCCM") * 2);
                        aTempDm.DmStatus = true;
                        aTempDm.ObjRunIndex = 2;
                        aTempDm.AddFormNameToDmObjectWhereUsedList("FrmCM");
                        break;
                    case "intResetACCCM1":

                        aTempDm.DmAddress = GetDmAddress(allTranslatedDMs, "intACCCM1");
                        aTempDm.DmCount = (short)(TryGetParameterIntValue("NumACCCM") * 2);
                        if (aTempDm.DmAddress > 0)
                        {
                            aTempDm.init_elem((short)TryGetParameterIntValue("NumACCCM"));
                        }
                        break;
                    case "intSideEqipStatusSystem":
                        aTempDm.DmAddress = TryGetParameterIntValue("SideEqipStatus_Addr");
                        aTempDm.DmCount = (short)TryGetParameterIntValue("SideEqipStatusDMCount");
                        aTempDm.DmStatus = true;
                        aTempDm.AddFormNameToDmObjectWhereUsedList("FrmSideEquipFLStatusDisp");  //周边状态运行查看
                        break;

                    #endregion 

                    default:
                        break;
                }
                #endregion 

                allTranslatedDMs.Add(aTempDm);
            }

            //根据条件加载到内存中
            allTranslatedDMs.ForEach((item) =>
            {
                //这里有个特殊的DM需要处理一下。
                if (item.DMObjectIndexName == "intXPUnloadStatus_R")
                {
                    if (intXPUnloadNo != 1)
                    {
                        AddToDmCollection(item);
                    }
                }
                else
                {
                    AddToDmCollection(item);
                }
            });
        }

        private static DmClass GetDm(List<DmClass> theAllDm, string theSourceDmIndex)
        {
            var aItem = theAllDm.Find(p => p.DMObjectIndexName == theSourceDmIndex);
            return aItem;
        }
         
        private static int GetDmAddress(List<DmClass> theAllDm, string theSourceDmIndex)
        {
            var aItem = GetDm(theAllDm, theSourceDmIndex);
            return aItem == null ? 0 : aItem.DmAddress;
        }

        private static short GetDmCount(List<DmClass> theAllDm, string theSourceDmIndex)
        {
            var aItem = GetDm(theAllDm, theSourceDmIndex); 
            return aItem == null ? (short)0 : aItem.DmCount;
        }

        private static void AddToDmCollection(DmClass theDm)
        {
            var aInsertIndex = GlobalCache.GetInstance.AddDmObject(theDm);
            theDm.DmObjectId = aInsertIndex;
        }

        #endregion 
    }
}

